package yxy.game.pm2.command.arena;

import yxy.cherry.data.bean.arenadate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ArenaMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.arena.ArenaHandle;
import yxy.game.pm2.module.arena.ArenaPlayerModule;
import yxy.game.pm2.module.hero.FormatHandle;

import java.util.Date;

/**
 * 竞技场 发起挑战
 */
@MessageType(proto = Command.ArenaChanllengeStart, classtype = ArenaMessage.ArenaChanllengeStartRequest.class)
public class CMD_ArenaChanllengeStart extends Command<ArenaMessage.ArenaChanllengeStartRequest> {

    @Override
    public void execute(MessageSource messageSource, ArenaMessage.ArenaChanllengeStartRequest request) {
        Date date = new Date();

        String playerCode = messageSource.getTag();
        String oppoCode = request.getCode();
        boolean skip = request.getSkip();

        ArenaMessage.ArenaChanllengeStartResponse.Builder response = ArenaMessage.ArenaChanllengeStartResponse.newBuilder();

        ArenaPlayerModule module = ArenaPlayerModule.get(playerCode);
        if (module == null) {
            send(messageSource, response, Result.ModuleNotOpen);// 竞技场模块没有开启
            return;
        }

        FormatHandle formatHandle = new FormatHandle();
        Result result = formatHandle.check(playerCode, formatHandle.get(playerCode, Format.FormatType.普通));
        if (!result.equals(Result.Success)) {
            send(messageSource, response, result);   //  重复武将判断
            return;
        }

        try {
            if (module.tryLock()) {
                if (module.times() <= 0) {
                    // 花道具
                    try {
                        arenadate data = new ArenaHandle().data(0);
                        Currency cost = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
                        new CurrencyHandle().cost(playerCode, CCReason.竞技场, cost);
                    } catch (CurrencyNotEnoughException e) {
                        send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                        return;
                    } catch (Exception e) {
                        send(messageSource, response, Result.DataError);
                        return;
                    }
                }
                if (!module.getOppo().contains(oppoCode)) {
                    send(messageSource, response, Result.ArenaTargetNotOppo);// 目标对手不在你的对手列表中
                    return;
                }
                module.setSkip(skip);
                result = new ArenaHandle().battleResponseExecute(module, oppoCode, response);
                send(messageSource, response, result);

            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            module.unlock();
        }

        Date date1 = new Date();
        System.out.println("CMD_ArenaChanllengeStart:" + (date1.getTime() - date.getTime()));

    }

    private void send(MessageSource messageSource, ArenaMessage.ArenaChanllengeStartResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}